package com.mobileshop.common;

import javax.servlet.ServletContext;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.mobileshop.android.controller.AddressController;
import com.mobileshop.android.controller.LoginController;
import com.mobileshop.android.controller.MainController;
import com.mobileshop.android.controller.MessageController;
import com.mobileshop.android.controller.OrderController;
import com.mobileshop.model.YAdress;
import com.mobileshop.model.YCategory;
import com.mobileshop.model.YOrder;
import com.mobileshop.model.YOrderItem;
import com.mobileshop.model.YProduct;
import com.mobileshop.model.YShop;
import com.mobileshop.web.controller.ProductController;
import com.mobileshop.web.controller.ShopController;
import com.mobileshop.web.controller.WebIndexController;

/**
 * API引导式配置
 */
public class DemoConfig extends JFinalConfig{
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		//加载少量必要配置，随后可用getProperty(...)获取值	
		loadPropertyFile("a_little_config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.FREE_MARKER);
		me.setError404View("/errors/error404.html");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		
		me.add("/login", LoginController.class);
		me.add("/address",AddressController.class);
		me.add("/main",MainController.class);
		me.add("/order",OrderController.class);
		me.add("/message",MessageController.class);
		me.add("/shop",ShopController.class);
		me.add("/webindex",WebIndexController.class);
		me.add("/product", ProductController.class);
		
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		
		//quartz插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setDialect(new OracleDialect());
		arp.setContainerFactory( new CaseInsensitiveContainerFactory());
		arp.addMapping("t_shop", "code", YShop.class);
		arp.addMapping("t_product_category", "code", YCategory.class);
		arp.addMapping("t_product", "code", YProduct.class);
		arp.addMapping("t_address", "code", YAdress.class);
		arp.addMapping("t_order", "code",YOrder.class);
		arp.addMapping("t_order_item","code", YOrderItem.class);
		me.add(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		
	}
	
	@Override
	public void afterJFinalStart(){
		ServletContext sc=JFinal.me().getServletContext();
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
